//
//  YCRequestGenerator.swift
//  YCNetworking
//
//  Created by YC on 2017/3/7.
//  Copyright © 2017年 YC. All rights reserved. mail:yangchong364@qq.com
//

import UIKit

class YCRequestGenerator {
    static let shared = YCRequestGenerator()
    private let headers: Dictionary<String,String>
    
    private init() {        
        let versionStr = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        
        self.headers = ["content-Type":"application/json; charset=utf-8",
                        "Version" : versionStr,
                        "Platform" : "2"]
    }
    
    func generateGetRequest(with serviceIdentifier: String, requestParams: Dictionary<String,Any>, methodName: String) -> URLRequest {
        
        let service = YCServiceFactory.shared.service(with: serviceIdentifier)
        
        var urlString: String
        
        if service.apiController.characters.count != 0 {
            urlString = service.apiBaseUrl + "/" + service.apiController + "/" + methodName
        }else {
            urlString = service.apiBaseUrl + "/" + methodName
        }
        
        let request = try! URLRequest(url: urlString, method: .get, headers: headers)
        
        return request
    }
    
    func generatePostRequest(with serviceIdentifier: String, requestParams: Dictionary<String,Any>, methodName: String) -> URLRequest {
        
        let service = YCServiceFactory.shared.service(with: serviceIdentifier)
        
        var urlString: String
        
        if service.apiController.characters.count != 0 {
            urlString = service.apiBaseUrl + "/" + service.apiController + "/" + methodName
        }else {
            urlString = service.apiBaseUrl + "/" + methodName
        }
        
        var request = try! URLRequest(url: urlString, method: .post, headers: headers)
        request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted)
        
        return request
    }
    
    func generatePutRequest(with serviceIdentifier: String, requestParams: Dictionary<String,Any>, methodName: String) -> URLRequest {
        
        let service = YCServiceFactory.shared.service(with: serviceIdentifier)
        
        var urlString: String
        
        if service.apiController.characters.count != 0 {
            urlString = service.apiBaseUrl + "/" + service.apiController + "/" + methodName
        }else {
            urlString = service.apiBaseUrl + "/" + methodName
        }
        
        var request = try! URLRequest(url: urlString, method: .put, headers: headers)
        request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted)
        
        return request
    }
    
    func generateDeleteRequest(with serviceIdentifier: String, requestParams: Dictionary<String,Any>, methodName: String) -> URLRequest {
        
        let service = YCServiceFactory.shared.service(with: serviceIdentifier)
        
        var urlString: String
        
        if service.apiController.characters.count != 0 {
            urlString = service.apiBaseUrl + "/" + service.apiController + "/" + methodName
        }else {
            urlString = service.apiBaseUrl + "/" + methodName
        }
        
        var request = try! URLRequest(url: urlString, method: .delete, headers: headers)
        request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: .prettyPrinted)
        
        return request
    }
}
